﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using PJ.Business;
using Microsoft.Practices.Composite.Events;

namespace PJ.Presentation
{
    public abstract class ItemViewModel : EntityViewModel, IItemModel
    {
        protected ItemViewModel()
        {
        }

        protected ItemViewModel(string globalId, IItemService service)
            : base(globalId, service)
        {
        }

        protected ItemViewModel(IBusinessObject obj, IItemService service)
            : base(obj, service)
        {
        }

        protected ItemViewModel(IView view, IEventAggregator aggregator, IItemService service)
            : base(view, aggregator, service)
        {
        }

        /// <summary>
        /// GetValues. Sets the businness object properties using the model property values
        /// </summary>
        /// <param name="businessObject">business object to update</param>
        public override IBusinessObject GetValues()
        {
            if (base.GetValues() == null)
            {
                if (IsStringNull(Name))
                    return null;
            }
            BusinessObject.Name = Name;
            return BusinessObject;
        }
        /// <summary>
        /// SetValues. Sets the model properties using the businessobject property values
        /// </summary>
        /// <param name="businessObject"></param>
        public override void SetValues(IBusinessObject businessObject, bool saveBefore)
        {
            base.SetValues(businessObject, saveBefore);
            if (businessObject == null)
                Name = null;
            else
                Name = BusinessObject.Name;
        }

        public string Name
        {
            get { return (string)ThreadUnsafeGetValue(NameProperty); }
            set { ThreadUnsafeSetValue(NameProperty, value); }
        }

        public static readonly DependencyProperty NameProperty =
            DependencyProperty.Register("Name", typeof(string), typeof(ItemViewModel), new UIPropertyMetadata(""));

        #region Protected

        protected override bool ValuesChanged()
        {
            if (base.ValuesChanged())
                return true;
            else
            {
                if (IsBusinessObjectNull)
                {
                    if (IsStringNull(Name))
                        return false;
                    else
                        return true;
                }
                if (StringEqual(Name, BusinessObject.Name))
                    return false;
                else
                    return true;
            }
        }
        protected new IItem BusinessObject
        {
            get { return base.BusinessObject as IItem; }
        }

        #endregion

        #region Field


        #endregion

    }
}
